﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;   
using System.Text;
using System.Windows.Forms;
using RuleCode.ObjectTree;
using System.IO;
using System.Xml;
using EnvDTE;
using Ionic.Zip;
using VSLangProj80;

namespace VSObjectTreeAddIn 
{
    public partial class AddSharpERPProject : Form
    {
        #region 构造方法
        public AddSharpERPProject()
        {
            InitializeComponent();
        }
        #endregion     

        #region 插件属性和字段
        /// <summary>
        /// 获取或设置 插件的宿主对象
        /// </summary>
        public DTE AddIn_ApplicationObject
        {
            get;
            set;
        }

        public bool  InitForm()
        {
            Util.InitCurrentProfileName();
            InitTreeView();
            if (TreeView_SolutionType.Nodes.Count <= 0)
            {
                this.Close();
                return false ;
            }
            if (AddIn_ApplicationObject != null && AddIn_ApplicationObject.Solution.Count > 0)
            {
                ComboBox_IsCreateSolution.Items.Clear();
                ComboBox_IsCreateSolution.Items.Add("创建新的解决方案");
                ComboBox_IsCreateSolution.Items.Add("添入解决方案中");
                if (CheckBox_CreateSolution.Checked)
                    ComboBox_IsCreateSolution.SelectedIndex = 0;
                else
                    ComboBox_IsCreateSolution.SelectedIndex = 1;
            }
            else
            {
                ComboBox_IsCreateSolution.Items.Clear();
                ComboBox_IsCreateSolution.Items.Add("创建新的解决方案");
                ComboBox_IsCreateSolution.SelectedIndex = 0;
                CheckBox_CreateSolution.Visible = false;
                ComboBox_IsCreateSolution.Enabled = false;
                //TextBox_SolutionName.Visible = false;
                //label4.Visible = false;
            }
            return true;
        }

        #endregion         

        #region 按钮事件
        private void Button_OK_Click(object sender, EventArgs e)
        {
            try
            {
                //Directory.CreateDirectory("D:\\testaa\\");
                //File.Create("D:\\testaa\\ddd.sln");
                //this.AddIn_ApplicationObject.Solution.Create("D:\\testaa", "ddd");
                
                ////this.AddIn_ApplicationObject.Solution.SaveAs("D:\\testaa\\ddd.sln");

                //return;

                bool isSuccess = false;
                if (ListView_SolutionTemplates.SelectedItems.Count > 0)
                {
                    string[] allInfos = (string[])ListView_SolutionTemplates.SelectedItems[0].Tag;
                    if (this.AddIn_ApplicationObject != null && this.AddIn_ApplicationObject.Solution.Count > 0)
                    {
                        if (CheckBox_CreateSolution.Checked)
                            isSuccess = CreateNewSolution();
                        else
                            isSuccess = AddToCurrentSolution();
                    }
                    else
                    {
                        isSuccess = CreateNewSolution();   
                    }
                }
                if (isSuccess)
                    this.Close();    
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message);
                Util.SetStatusInfo(error.Message);
                this.Close();
            }
        }


        private void button_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 窗体加载事件
        private void AddSharpERPProject_Load(object sender, EventArgs e)
        {
            
        }
        #endregion

        #region 添加或者创建新的项目
        protected bool AddToCurrentSolution()
        {
            bool isSuccess = false;
            if (this.AddIn_ApplicationObject .Solution .Count >0 &&  !String.IsNullOrEmpty(TextBox_SolutionName.Text.Trim()) && !String.IsNullOrEmpty(TextBox_ProjectName.Text)
                && ComboBox_SolutionPostion.SelectedItem != null && !String.IsNullOrEmpty(ComboBox_SolutionPostion.SelectedItem.ToString()))
            {
                string[] allInfos = (string[])ListView_SolutionTemplates.SelectedItems[0].Tag;
                string solutionFullName = this.AddIn_ApplicationObject.Solution.FullName;
                FileInfo fInfo = new FileInfo(solutionFullName);
                string solutionPath = fInfo.Directory .FullName ;
                this.AddIn_ApplicationObject.Solution.AddFromTemplate(allInfos[0], solutionPath + "\\" + TextBox_ProjectName.Text, TextBox_ProjectName.Text, false);
                Project project =  Util_DTE.FindProject(TextBox_ProjectName.Text);
                if (project != null)
                {
                    ImportProjectReferrence(project, allInfos[0]);
                    project.Save(project.FullName);
                    ImportTemplateConfigure(allInfos[0], TextBox_ProjectName.Text, Guid.NewGuid().ToString("N"));
                    isSuccess = true;
                }
            }
            return isSuccess;
        }

        protected bool CreateNewSolution()
        {
            if(this.AddIn_ApplicationObject .Solution .Count >0)
                this.AddIn_ApplicationObject.Solution.Close(true);

            bool isSuccess = false;
            //创建新的项目
            if (!String.IsNullOrEmpty(TextBox_SolutionName.Text.Trim()) && !String.IsNullOrEmpty(TextBox_ProjectName.Text)
                && ComboBox_SolutionPostion.SelectedItem != null && !String.IsNullOrEmpty(ComboBox_SolutionPostion.SelectedItem.ToString()))
            {
                string[] allInfos = (string[])ListView_SolutionTemplates.SelectedItems[0].Tag;
                string solutionPath = ComboBox_SolutionPostion.SelectedItem.ToString() + "\\" + TextBox_SolutionName.Text;
                if (!Directory.Exists(solutionPath))
                    Directory.CreateDirectory(solutionPath);
                string solutionFileName = solutionPath + "\\" + TextBox_SolutionName.Text + ".sln";
                if (File.Exists(solutionFileName))
                    File.Delete (solutionFileName);

                this.AddIn_ApplicationObject.Solution.Create(solutionPath, TextBox_SolutionName.Text);
                try
                {
                    this.AddIn_ApplicationObject.Solution.SaveAs(solutionFileName);
                }
                catch { }

                this.AddIn_ApplicationObject.Solution.AddFromTemplate(allInfos[0], solutionPath + "\\" + TextBox_ProjectName.Text, TextBox_ProjectName.Text, false);
                Project project = this.AddIn_ApplicationObject.Solution.Item(1);
                ImportProjectReferrence(project, allInfos[0]);
                project.SaveAs(project.FullName);
                ImportTemplateConfigure(allInfos[0], TextBox_ProjectName.Text ,Guid.NewGuid ().ToString ("N"));
                isSuccess = true;
            }
            else
            {
                MessageBox.Show("项目名称和解决方案名称不能为空！");
                Util.SetStatusInfo("项目名称和解决方案名称不能为空！");
                isSuccess = false;
            }
            return isSuccess;
        }

        /// <summary>
        /// 导入项目的相关引用
        /// </summary>
        /// <param name="project">对应的项目</param>
        /// <param name="templateFileFullName">模板文件</param>
        protected void ImportProjectReferrence(Project project ,string templateFileFullName)
        {
            if (project == null)
                return;

            FileInfo templateInfo = new FileInfo(templateFileFullName);
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(templateFileFullName);
            XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlDoc.NameTable);
            namespaceManager.AddNamespace("DD", "http://schemas.microsoft.com/developer/vstemplate/2005");
            XmlNode projectTypeNode = xmlDoc.SelectSingleNode("//DD:config/DD:ProjectType",namespaceManager );


            string ruleCodeCommonDllFileName = Path.Combine(Util_DTE.AddIn_SetUpPath, "RuleCode.Common.dll");
            string ruleCodeObjectTreeFileName = Path.Combine(Util_DTE.AddIn_SetUpPath, "RuleCode.ObjectTree.dll");
            string ajaxProFileName = Path.Combine(Util_DTE.AddIn_SetUpPath, "AjaxPro.2.dll");
            string webControlsFileName = Path.Combine(Util_DTE.AddIn_SetUpPath, "RuleCode.WebControls.dll");
                                        
            VSProject2 vsDataProject2 = project.Object as VSProject2;

            if (projectTypeNode != null && vsDataProject2 != null)
            {

                switch (projectTypeNode.InnerText)
                {
                    case "Data":
                        {                               
                            if (File.Exists(ruleCodeCommonDllFileName))
                                vsDataProject2.References.Add(ruleCodeCommonDllFileName);
                            if (File.Exists(ruleCodeObjectTreeFileName))
                                vsDataProject2.References.Add(ruleCodeObjectTreeFileName); 
                            break;
                        }
                    case "WebForm":
                        {       
                            if (File.Exists(ruleCodeCommonDllFileName))
                                vsDataProject2.References.Add(ruleCodeCommonDllFileName);
                            if (File.Exists(ruleCodeObjectTreeFileName))
                                vsDataProject2.References.Add(ruleCodeObjectTreeFileName);
                            if (File.Exists(ajaxProFileName))
                                vsDataProject2.References.Add(ajaxProFileName);
                            if (File.Exists(webControlsFileName))
                                vsDataProject2.References.Add(webControlsFileName);   
                            break;
                        }
                    case "WinForm":
                        {               
                            if (File.Exists(ruleCodeCommonDllFileName))
                                vsDataProject2.References.Add(ruleCodeCommonDllFileName);
                            if (File.Exists(ruleCodeObjectTreeFileName))
                                vsDataProject2.References.Add(ruleCodeObjectTreeFileName);     
                            break;
                        }
                    default: break;
                }
            }
        }

        /// <summary>
        /// 导入项目模板的配置
        /// </summary>
        /// <param name="templateFileName"></param>
        /// <param name="curProjectName"></param>
        /// <param name="defualtConfigName"></param>
        protected void ImportTemplateConfigure(string templateFileName, string curProjectName,string defualtConfigName)
        {
            Util.InitCurrentProfileName(); //初始化配置信息
            //string sharpERPPath = Util.GetUserDataPath();
            //string aotsPath = Path.Combine(sharpERPPath, GlobalStaticDirectoryPath.AOTSProfilePath.Trim ("\\".ToCharArray ()));         
            //string configPath = Path.Combine(aotsPath, configname); 
            //if (CheckBox_ImportTempConfig.Checked)
            {
               

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(templateFileName);
                XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlDoc.NameTable);
                namespaceManager.AddNamespace("DD", "http://schemas.microsoft.com/developer/vstemplate/2005");
                XmlNode pathsNode = xmlDoc.SelectSingleNode("//DD:config/DD:paths", namespaceManager);
                XmlNode rootnamespaceNode = xmlDoc.SelectSingleNode("//DD:config/DD:RootNameSpace", namespaceManager);

                string solutionName = this.AddIn_ApplicationObject.Solution.FullName;
                XmlDocument solutionsDoc = new XmlDocument();
                solutionsDoc.Load(Util.GetSolutionConfigPath());
                XmlNode solutionNode = solutionsDoc.SelectSingleNode("//solution[@Path='" + solutionName + "']");
                if (solutionNode != null)
                {
                    string ProfileName = string.Empty;
                    XmlNode ProfileNameNode = solutionNode.Attributes.GetNamedItem("ProfileName");
                    if (ProfileNameNode == null || ProfileNameNode.InnerText.Trim() == string.Empty)
                    {
                        if (ProfileNameNode == null)
                        {
                            ProfileNameNode = solutionsDoc.CreateAttribute("ProfileName");
                        }
                        string newProfileName = defualtConfigName;
                        ProfileNameNode.Value = newProfileName;// AddIn_ApplicationObject.Solution.FileName.ToLower().TrimEnd(".sln".ToCharArray());
                        solutionNode.Attributes.Append(ProfileNameNode as XmlAttribute);

                        //在AOTS下创建相应的文件
                        string aotsPath = Path.Combine(Util.GetUserDataPath(), GlobalStaticDirectoryPath.AOTSProfilePath.Trim("\\".ToCharArray()));
                        string profilePath = Path.Combine(aotsPath, newProfileName);
                        if (!Directory.Exists(profilePath))
                            Directory.CreateDirectory(profilePath);
                        string devPath = Path.Combine(profilePath, Util.DevArea);
                        if (!Directory.Exists(devPath))
                            Directory.CreateDirectory(devPath);
                        string setupDataFilePath = Path.Combine(Util_DTE.AddIn_SetUpPath, "Data");

                        //解压代码生成模板
                        string sourceGeneTemplateZipFileName = Path.Combine(setupDataFilePath, "Templates.zip");
                        using (ZipFile geneTemplateZip = ZipFile.Read(sourceGeneTemplateZipFileName))
                        {
                            foreach (ZipEntry entry in geneTemplateZip)
                                entry.Extract(devPath, ExtractExistingFileAction.OverwriteSilently);
                        }
                        //解压LabelFile文件

                        string sourceLabelFileZipFileName = Path.Combine(setupDataFilePath, "LabelFile.zip");
                        using (ZipFile labelFileZip = ZipFile.Read(sourceLabelFileZipFileName))
                        {
                            foreach (ZipEntry entry in labelFileZip)
                                entry.Extract(devPath, ExtractExistingFileAction.OverwriteSilently);
                        }
                    }
                    ProfileName = ProfileNameNode.Value;

                    XmlDocument profilesDoc = new XmlDocument();
                    profilesDoc.Load(Util.GetProfilePath());
                    XmlNode aotsNode = profilesDoc.SelectSingleNode("//aots");
                    XmlNode aotNode = profilesDoc.SelectSingleNode("//aot[@Name='" + ProfileName + "']");
                    if (aotNode == null && aotsNode != null)
                    {
                        aotNode = profilesDoc.CreateElement("aot");
                        XmlAttribute aotNameNode = profilesDoc.CreateAttribute("Name");
                        aotNameNode.Value = ProfileName;
                        aotNode.Attributes.Append(aotNameNode);
                        XmlAttribute cnnStrNode = profilesDoc.CreateAttribute("cnnStringID");
                        cnnStrNode.Value = string.Empty ;
                        aotNode.Attributes.Append(cnnStrNode);
                        aotNode.Attributes.Append(profilesDoc.CreateAttribute("OTPath_Module"));
                        aotNode.Attributes.Append(profilesDoc.CreateAttribute("OTPath"));
                        aotsNode.AppendChild(aotNode);
                        profilesDoc.Save(Util.GetProfilePath());
                    }

                    CopyTemplateNodeInfoToSolutionNode(namespaceManager, curProjectName, xmlDoc, solutionsDoc, "//DD:config/DD:paths/DD:Tables", "//solution[@Path='" + solutionName + "']/paths/Tables", true);
                    CopyTemplateNodeInfoToSolutionNode(namespaceManager, curProjectName, xmlDoc, solutionsDoc, "//DD:config/DD:paths/DD:Queries", "//solution[@Path='" + solutionName + "']/paths/Queries", true);
                    CopyTemplateNodeInfoToSolutionNode(namespaceManager, curProjectName, xmlDoc, solutionsDoc, "//DD:config/DD:paths/DD:TableFields", "//solution[@Path='" + solutionName + "']/paths/TableFields", true);
                    CopyTemplateNodeInfoToSolutionNode(namespaceManager, curProjectName, xmlDoc, solutionsDoc, "//DD:config/DD:paths/DD:ExtendDataTypes", "//solution[@Path='" + solutionName + "']/paths/ExtendDataTypes", true);
                    CopyTemplateNodeInfoToSolutionNode(namespaceManager, curProjectName, xmlDoc, solutionsDoc, "//DD:config/DD:paths/DD:Enums", "//solution[@Path='" + solutionName + "']/paths/Enums", true);
                    CopyTemplateNodeInfoToSolutionNode(namespaceManager, curProjectName, xmlDoc, solutionsDoc, "//DD:config/DD:paths/DD:WebForms", "//solution[@Path='" + solutionName + "']/paths/WebForms", true);
                    CopyTemplateNodeInfoToSolutionNode(namespaceManager, curProjectName, xmlDoc, solutionsDoc, "//DD:config/DD:paths/DD:Classes", "//solution[@Path='" + solutionName + "']/paths/Classes", true);
                    CopyTemplateNodeInfoToSolutionNode(namespaceManager, curProjectName, xmlDoc, solutionsDoc, "//DD:config/DD:paths/DD:Forms", "//solution[@Path='" + solutionName + "']/paths/Forms", true);
                    CopyTemplateNodeInfoToSolutionNode(namespaceManager, curProjectName, xmlDoc, solutionsDoc, "//DD:config/DD:paths/DD:LabelFile", "//solution[@Path='" + solutionName + "']/paths/LabelFile", true);
                    CopyTemplateNodeInfoToSolutionNode(namespaceManager, curProjectName, xmlDoc, solutionsDoc, "//DD:config/DD:paths/DD:AOTFile", "//solution[@Path='" + solutionName + "']/paths/AOTFile", true);


                    CopyTemplateNodeInfoToSolutionNode(namespaceManager, curProjectName, xmlDoc, solutionsDoc, "//DD:RootNameSpace/DD:Classes", "//solution[@Path='" + solutionName + "']/RootNameSpace/Classes", false );
                    CopyTemplateNodeInfoToSolutionNode(namespaceManager, curProjectName, xmlDoc, solutionsDoc, "//DD:RootNameSpace/DD:WebForms", "//solution[@Path='" + solutionName + "']/RootNameSpace/WebForms", false );
                    CopyTemplateNodeInfoToSolutionNode(namespaceManager, curProjectName, xmlDoc, solutionsDoc, "//DD:RootNameSpace/DD:Forms", "//solution[@Path='" + solutionName + "']/RootNameSpace/Forms", false );
                    solutionsDoc.Save(Util.GetSolutionConfigPath());
                }
            }
        }

        protected void CopyTemplateNodeInfoToSolutionNode(XmlNamespaceManager templNamespaceManager, string curProjectName, XmlDocument templateDoc, XmlDocument solutionDoc, string templateNodePath, string solutionNodePath, bool isPath)
        {
            XmlNode templateNode = templateDoc.SelectSingleNode(templateNodePath,templNamespaceManager );
            XmlNode solutionNode = solutionDoc.SelectSingleNode(solutionNodePath);
            if (solutionNode != null && templateNode != null)
            {
                if (isPath)   //Paths 节点
                {
                    XmlNode temProjectAttr = templateNode.Attributes.GetNamedItem("Project");
                    XmlNode solProjectAttr = solutionNode.Attributes.GetNamedItem("Project");
                    if (temProjectAttr != null && solProjectAttr != null)
                    {
                        if (temProjectAttr.Value == "$")
                            solProjectAttr.Value = curProjectName;
                        else
                            solProjectAttr.Value = temProjectAttr.Value;
                    }
                    XmlNode temValueAttr = templateNode.Attributes.GetNamedItem("Value");
                    XmlNode solValueAttr = solutionNode.Attributes.GetNamedItem("Value");
                    if (temValueAttr != null && solValueAttr != null)
                    {
                        solValueAttr.Value = temValueAttr.Value;
                    }
                }
                else     // RootNameSpace 节点
                {
                    if (templateNode.InnerText.Trim() == "$")
                        solutionNode.InnerText = curProjectName;
                    else
                        solutionNode.InnerText = templateNode.InnerText;
                }
            }
        }
        #endregion

        #region 模板类型改变事件
        private void TreeView_SolutionType_AfterSelect(object sender, TreeViewEventArgs e)
        {
            e.Node.Expand ();
            imageList2.Images.Clear();
            string solutionPath = Util_DTE.TemplatePath_Projects +e.Node.FullPath  ;
            FileInfo[] allSolutionFiles = Util_DTE.SearchFiles(solutionPath, "MyTemplate.vstemplate");
            ListView_SolutionTemplates.Items.Clear();    
            foreach (FileInfo childInfo in allSolutionFiles)
            {
                string key = childInfo.FullName;
                string text = childInfo.Name;
                string desc = childInfo.Name;
                string imagePath = string.Empty;
                string name = string.Empty;
                int itemImageIndex = 0;
                try {
                    XmlDocument xmlDocument = new XmlDocument();
                    xmlDocument.Load(childInfo.OpenRead());
                    XmlNode dataNode = xmlDocument.GetElementsByTagName("VSTemplate")[0];
                    string namespaceStr = dataNode.Attributes["xmlns"].Value;
                    XmlNamespaceManager xmlNameManager = new XmlNamespaceManager(xmlDocument.NameTable  );
                    xmlNameManager.AddNamespace("ab", namespaceStr);
                    XmlNode childNode = dataNode.SelectNodes("//ab:Name", xmlNameManager)[0];
                    if (childNode != null)
                        name = childNode.InnerText;
                    childNode = dataNode.SelectNodes("//ab:Description", xmlNameManager)[0];
                    if (childNode != null)
                        desc = childNode.InnerText ;
                    text = name;

                    XmlNodeList icoNodes = dataNode.SelectNodes("//ab:Icon",xmlNameManager );
                    if (icoNodes.Count > 0)
                    {
                        string icoFilePath = childInfo.Directory+"\\" + icoNodes[0].InnerText;
                        if (File.Exists(icoFilePath))
                        {
                            imageList2.Images.Add(Image .FromFile (icoFilePath ));
                            itemImageIndex = imageList2.Images.Count - 1;
                        }
                    }
                }
                catch { }

                ListViewItem item = new ListViewItem(ListView_SolutionTemplates.Groups["listViewGroup1"]);
                item.Text = text;
                item.ImageIndex = itemImageIndex;
                item.Name = childInfo.FullName;     
                item.Group = ListView_SolutionTemplates.Groups["listViewGroup1"];
                item.Tag = new string[] { childInfo.FullName, desc, name };                        
                 
                ListView_SolutionTemplates.Items.Add(item);           
            }
            if (imageList2.Images.Count <= 0)
                imageList2.Images.Add(imageList1.Images[0]);
        }
        #endregion

        #region 初始化
        private void InitTreeView()
        {
            TreeView_SolutionType.Nodes.Clear();
            if (Util_DTE.AddIn_SetUpPath != null)
            {                                                                   
                string solutionTemplatePath = Util_DTE.TemplatePath_Projects  ;
                DirectoryInfo dInfo = new DirectoryInfo(solutionTemplatePath);
                if (dInfo.Exists)
                {
                    DirectoryInfo[] childDInfos = dInfo.GetDirectories();
                    foreach (DirectoryInfo childInfo in childDInfos)
                    {
                        TreeNode treeNode = new TreeNode(childInfo.Name);
                        DirectoryInfo[] childChildInfos = childInfo.GetDirectories();
                        foreach (DirectoryInfo childChildInfo in childChildInfos)
                        {
                            TreeNode childTreeNode = new TreeNode(childChildInfo.Name);
                            treeNode.Nodes.Add(childTreeNode);
                        }
                        TreeView_SolutionType.Nodes.Add(treeNode);
                    }
                }
                else
                {
                    string message = "对不起，没有找到项目模板路径:" + solutionTemplatePath  ;
                    MessageBox.Show(message);
                    Util.SetStatusInfo(message);
                    return;
                }

            }
            if (TreeView_SolutionType.Nodes.Count <= 0)
            {
                string message = "对不起，在安装目录下没有找到相应的项目模板！";
                MessageBox.Show(message);
                Util.SetStatusInfo(message);                                         
                return;
            }
            TreeView_SolutionType.SelectedNode = TreeView_SolutionType.Nodes[0];
            TreeView_SolutionType.ExpandAll ();
        }
        #endregion

        #region 相关事件
        private void Button_SelectPositionDialog_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog_Position.ShowDialog();
            string filePath = FolderBrowserDialog_Position.SelectedPath;
            int index = ComboBox_SolutionPostion.Items.IndexOf(filePath);
            if (index < 0)
                ComboBox_SolutionPostion.Items.Add(filePath);
            ComboBox_SolutionPostion.SelectedItem = filePath;
        }

        private void ListView_SolutionTemplates_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ListView_SolutionTemplates.SelectedItems.Count > 0)
            {
                string[] allInfos = (string[])ListView_SolutionTemplates.SelectedItems[0].Tag;
                Label_TemplateInfo.Text = allInfos [1];
                TextBox_ProjectName.Text = allInfos[2];
                if(CheckBox_CreateSolution .Checked )
                    TextBox_SolutionName.Text = allInfos[2];
            }
        }

        private void ComboBox_IsCreateSolution_SelectedIndexChanged(object sender, EventArgs e)
        {
            CheckBox_CreateSolution.Checked = ComboBox_IsCreateSolution.SelectedIndex == 0;
            TextBox_SolutionName.Enabled = CheckBox_CreateSolution.Checked;
            
            //加入解决方案
            if (!CheckBox_CreateSolution.Checked)
            {
                if (this.AddIn_ApplicationObject.Solution.Count > 0)
                {
                    string solutionFileName = this.AddIn_ApplicationObject.Solution.FullName;
                    FileInfo fInfo = new FileInfo(solutionFileName);
                    TextBox_SolutionName.Text = fInfo.Name.TrimEnd(fInfo.Extension.ToCharArray());
                    int index = ComboBox_SolutionPostion.Items.IndexOf(fInfo.Directory.FullName);
                    if (index < 0)
                    {
                        ComboBox_SolutionPostion.Items.Add(fInfo.Directory.FullName);
                        index = ComboBox_SolutionPostion.Items.Count - 1;
                    }
                    ComboBox_SolutionPostion.SelectedIndex = index;
                }
            }
        }

        private void CheckBox_CreateSolution_CheckedChanged(object sender, EventArgs e)
        {
            ComboBox_IsCreateSolution.SelectedIndex = (CheckBox_CreateSolution.Checked) ? 0 : 1;
            TextBox_SolutionName.Enabled = CheckBox_CreateSolution.Checked;

            //加入解决方案
            if (!CheckBox_CreateSolution.Checked)
            {
                if (this.AddIn_ApplicationObject.Solution.Count > 0)
                {
                    string solutionFileName = this.AddIn_ApplicationObject.Solution.FullName;
                    FileInfo fInfo = new FileInfo(solutionFileName);
                    TextBox_SolutionName.Text = fInfo.Name.TrimEnd (fInfo.Extension .ToCharArray ());
                    int index = ComboBox_SolutionPostion.Items.IndexOf(fInfo.Directory.FullName);
                    if (index < 0)
                    {
                        ComboBox_SolutionPostion.Items.Add(fInfo.Directory.FullName);
                        index = ComboBox_SolutionPostion.Items.Count - 1;
                    }
                    ComboBox_SolutionPostion.SelectedIndex = index;
                }
            }
        }
        #endregion
    }
}
